package me.monster.smarttraffic.net;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import io.reactivex.Observable;
import io.reactivex.functions.Function;
import me.monster.smarttraffic.bo.BusDistance;
import me.monster.smarttraffic.bo.RoadLight;
import me.monster.smarttraffic.tool.RxUtils;

/**
 * @description
 * @author: Created jiangjiwei in 2020-02-11 15:00
 */
public class RemoteService {
    private RemoteService() {

    }

    public static RemoteService getInstance() {
        return SingletonInstance.INSTANCE;
    }

    public Observable<Map<String, Object>> login(String account, String password) {
        return getService().login(account, password).compose(RxUtils.transform());
    }

    /**
     * 获取道路环境信息
     *
     * @return 环境信息
     */
    public Observable<Map<String, String>> fetchRoadEnvironment() {
        return getService().roadEnvironment().compose(RxUtils.transform()).map(new Function<Map<String, String>, Map<String, String>>() {
            @Override
            public Map<String, String> apply(Map<String, String> stringStringMap) throws Exception {
                Random random = new Random();
                for (String s : stringStringMap.keySet()) {
                    stringStringMap.put(s, String.valueOf(random.nextInt(50)));
                }
                return stringStringMap;
            }
        });
    }


    /**
     * 公交车距离站台距离信息
     *
     * @return 距离信息
     */
    public Observable<Map<String, String>> fetchBusDistance() {
        return getService().busDistance()
                .compose(RxUtils.transform())
                .map(new Function<BusDistance, Map<String, String>>() {
                    @Override
                    public Map<String, String> apply(BusDistance busDistance) throws Exception {
                        Map<String, String> allDistance = new HashMap<>(5);
                        List<String> bus1 = busDistance.getBus1();
                        Random random = new Random();
                        for (int i = 0; i < bus1.size(); i++) {
                            allDistance.put("bus1_" + (i + 1), String.valueOf(random.nextInt(100)));
                        }

                        List<String> bus2 = busDistance.getBus2();
                        for (int i = 0; i < bus2.size(); i++) {
                            allDistance.put("bus2_" + (i + 1), String.valueOf(random.nextInt(100)));
                        }
                        return allDistance;
                    }
                });
    }

    /**
     * 列出所有的车辆信息
     *
     * @return 车辆信息 List 包包含 车辆编号
     */
    public Observable<List<String>> listCar() {
        return getService().listCar().compose(RxUtils.transform());
    }

    /**
     * 查询小车账户余额
     *
     * @param no 小车编号
     * @return 小车信息
     */
    public Observable<Map<String, String>> queryCar(String no) {
        return getService().quryCar(no).compose(RxUtils.transform());
    }

    /**
     * 小车充值
     *
     * @param no     编号
     * @param charge 充值钱
     * @return 是否充值成功
     */
    public Observable<Boolean> chargeCar(String no, String charge) {
        return getService().chargeCar(no, charge).compose(RxUtils.transform());
    }


    /**
     * 停车状态
     *
     * @return
     */
    public Observable<Map<String, Boolean>> parkingState() {
        return getService().parkingState().compose(RxUtils.transform());
    }

    /**
     * 获取停车场费率
     *
     * @return
     */
    public Observable<String> getParkingPrice() {
        return getService().parkingPrice().compose(RxUtils.transform());
    }

    /**
     * 设置停车场费率
     *
     * @param price
     * @return
     */
    public Observable<Boolean> setParkingPrice(String price) {
        return getService().setParkingPrice(price).compose(RxUtils.transform());
    }

    /**
     * 站台上的公交距离
     *
     * @param stationNo
     * @return
     */
    public Observable<Map<String, Integer>> busDistanceByStation(int stationNo) {
        return getService().busDistanceByStation(stationNo).compose(RxUtils.transform());
    }

    /**
     * 站台上的环境指标
     *
     * @param stationNo
     * @return
     */
    public Observable<Map<String, String>> busEnvByStation(int stationNo) {
        return getService().busEnvironmentByStation(stationNo).compose(RxUtils.transform());
    }

    /**
     * 路灯排序规则
     *
     * @return
     */
    public Observable<List<String>> listLightRule() {
        return getService().listRoadLightRule().compose(RxUtils.transform());
    }

    /**
     * 路灯信息
     *
     * @return
     */
    public Observable<List<RoadLight>> listLight() {
        return getService().listRoadLight().compose(RxUtils.transform());
    }

    /**
     * 环境指标 - air
     *
     * @return
     */
    public Observable<Map<String, Integer>> envAir() {
        return getService().envAir().compose(RxUtils.transform());
    }

    /**
     * 环境指标 - air
     *
     * @return
     */
    public Observable<Map<String, Integer>> envLight() {
        return getService().envLight().compose(RxUtils.transform());
    }


    private Api getService() {
        return RequestManager.getInstance().getService();
    }

    private static class SingletonInstance {
        private static final RemoteService INSTANCE = new RemoteService();
    }
}
